<template>
    <section class="showChose" v-show="innerVisible" @click="innerVisible=false">
      <section class="address" @click.stop="noop">
        <section class="top-title">
          <h4>所在地区</h4>
          <span @click="innerVisible=false">×</span>
        </section>
        <section class="title">        
          <div class="area" @click="showTab(index)" :class="{active:index==currentIndex}" v-for="(value,index) in topValues" :key="index">{{value.title||'请选择'}}</div>
        </section>
        <ul class="city-list" v-for="(items,infoIndex) in info" :key="infoIndex" v-show="infoIndex==currentIndex">
           <li class="addList" @click="selectArea(item)" v-for="item in items" :key="item.id" 
            :class="{active:values[infoIndex] && item.id==values[infoIndex]}">{{item.title}}</li>
        </ul>
      </section>
    </section>
</template>
<script>
export default {
  name: "city-selector",
  model: {
    event: "onSelectedAddrChange",
    prop: "selectedAddr"
  },
  props: {
    selectedAddr: "",
    visible: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    visible(v) {
      this.innerVisible = v;
    },
    innerVisible(v) {
      this.$emit("on-visible-change", v);
    }
  },
  created() {
    this.loadData(1);
  },
  data() {
    return {
      innerVisible: this.visible,
      currentIndex: 0,
      topValues: [
        {
          title: "",
          id: 0
        }
      ],
      values: [],
      titles: [],
      info: []
    };
  },
  methods: {
    loadData(level, pid, callback) {
      level = level || 1;
      pid = pid || 0;
      this.$set(this.info, level - 1, []);
      this.$get("/api/base_address/list", {
        level,
        pid
      }).then(data => {
        if (data && data.length) {
          data.forEach(element => {
            element.level = level;
          });
          this.$set(this.info, level - 1, data);
        }
        callback(data);
      });
    },
    showTab(index) {
      this.currentIndex = index;
    },
    selectArea(item) {
      let index = item.level - 1; //选择的第几列

      this.topValues.splice(index + 1);
      this.values.splice(index);
      this.titles.splice(index);
      //选择项目
      this.topValues[this.topValues.length - 1].title = item.title;

      this.values.push(item.id);
      this.titles.push(item.title);

      if (index + 1 >= 4) {
        this.$emit("onSelectedAddrChange", this.titles);
        this.$emit("onSelectedAddrTextChange", this.titles);
        this.innerVisible = false;
        return;
      }

      this.loadData(item.level + 1, item.id, res => {
        if (res && res.length) {
          this.currentIndex = index + 1;
          this.topValues.push({
            title: "",
            id: 0
          });
        } else {
          this.$emit("onSelectedAddrChange", this.titles);
          this.$emit("onSelectedAddrTextChange", this.titles);
          this.innerVisible = false;
        }
      });
    }
  }
};
</script>
<style scoped>
.city-list {
  list-style: none;
  padding: 0;
}
.top-title {
  text-align: center;
  font-size: 0.34rem;
  position: relative;
}
.top-title span {
  position: absolute;
  top: -0.15rem;
  right: 0.4rem;
  font-size: 0.5rem;
}
.myAddress {
  width: 100%;
  background-color: white;
  border-top: 4px solid rgba(245, 245, 245, 1);
  color: #333;
}
.myAddress .cont {
  border-bottom: 1px solid rgba(245, 245, 245, 0.8);
}
.myAddress .cont span {
  display: inline-block;
  font-size: 0.28rem;
  color: #333;
  line-height: 0.88rem;
  margin-left: 0.32rem;
}
.myAddress .cont section {
  float: left;
}
.myAddress .cont p {
  display: inline-block;
  font-size: 0.28rem;
  color: #333333;
  line-height: 0.88rem;
  margin-left: 1rem;
}
.myAddress .cont .pic2 {
  float: right;
  width: 0.14rem;
  height: 0.24rem;
  margin: 0.32rem 0.32rem 0.32rem 0;
}
.myAddress .cont p.text {
  margin-left: 0.72rem;
}
.showChose {
  width: 100%;
  height: 100%;
  z-index: 99999;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.36);
}
.address {
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 121;
  background: #fff;
  width: 100%;
}

.area {
  display: inline-block;
  font-size: 0.34rem;
  line-height: 0.88rem;
  margin-left: 0.42rem;
  color: #333;
}
.addList {
  font-size: 0.34rem;
  line-height: 0.88rem;
  color: #333;
}
/* 修改的格式 */
.address ul {
  height: 4.4rem;
  overflow: auto;
}
.address ul li {
  padding-left: 0.4rem;
  padding-right: 0.4rem;
}
.address .title .active {
  color: #0071b8;
  border-bottom: 0.02rem solid #0071b8;
}
.address ul .active {
  color: #0071b8;
}
</style>
